package com.wwwarehouse.taskcenter.fragment.warehouse_reg;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.StorageBillAdapter;
import com.wwwarehouse.taskcenter.bean.StorageBillBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.eventbus_event.StorageBillEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * Created by clannad.shao on 2017/11/4.
 *
 * @author clannad_sk
 * @date 2017/11/4
 * 订单跟踪fragment
 */
public class StorageBillItemFragment extends BaseFragment implements StorageBillAdapter.OnItemClickListener {

    private RecyclerView idStorageBillRv;
    private ArrayList<StorageBillBean.StorageBillItemBean> storageBillList;
    private boolean isMeasured = false;
    private static final int GET_BILL_REQUEST_CODE = 0x45;
    private static final int GET_BILL_PAGESIZE = 10;
    private static final String REQUEST_SUCCESS_CODE = "0";
    private StorageBillAdapter mAdapter;
    private String searchText;
    private Map<String, Object> reqParamMap;
    private List<String> businessIdList;
    private List<String> ownerList;
    private List<String> statusList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.task_center_getbill_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化view
        idStorageBillRv = $(R.id.idStorageBillRv);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof StorageBillItemFragment) {
            mActivity.setTitle(getString(R.string.warehouse_reg_title));
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        initData();
        return null;
    }

    public void initData() {
        searchText = "";
        idStorageBillRv.addItemDecoration(new SpaceItemDecoration(0, 0));
        idStorageBillRv.setLayoutManager(new GridLayoutManager(mActivity, 1));
        Bundle bundle = getArguments();
        if (null != bundle) {
            storageBillList = bundle.getParcelableArrayList("dataList");
            int page = bundle.getInt("page");
            searchText = bundle.getString("searchText");
            businessIdList = (List<String>) bundle.getSerializable("businessIdList");
            ownerList = (List<String>) bundle.getSerializable("ownerList");
            statusList = (List<String>) bundle.getSerializable("statusList");
            if (storageBillList != null && page == 1) {
                ViewTreeObserver observer = idStorageBillRv.getViewTreeObserver();
                observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        if (!isMeasured) {
                            setHeight(idStorageBillRv.getMeasuredHeight());
                        }
                        return true;
                    }
                });
            } else {
                requestHttp(page, searchText, businessIdList, ownerList, statusList);
            }
        }
    }

    public void requestHttp(int page, String searchText, List<String> businessIdList, List<String> ownerList, List<String> statusList) {
        if(reqParamMap == null){
            reqParamMap = new HashMap<>();
        }
        reqParamMap.put("pageNo", page);
        reqParamMap.put("pageSize", GET_BILL_PAGESIZE);
        reqParamMap.put("searchValue", searchText);
        reqParamMap.put("businessIdList", businessIdList);
        reqParamMap.put("ownerList", ownerList);
        reqParamMap.put("statusList", statusList);
        loadData(TaskCenterConstant.GET_STORAGE_BILL_LIST, reqParamMap, GET_BILL_REQUEST_CODE);
    }

    public void loadData(String url, Map<String, Object> map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                if (i == GET_BILL_REQUEST_CODE) {
                    if (REQUEST_SUCCESS_CODE.equals(commonClass.getCode())) {
                        StorageBillBean bean =
                                JSON.parseObject(commonClass.getData().toString(), StorageBillBean.class);
                        if (bean != null) {
                            setData(bean);
                        }
                    }
                } else {

                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }

    private void setData(StorageBillBean bean) {
        storageBillList = bean.getList();
        ViewTreeObserver observer = idStorageBillRv.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (!isMeasured) {
                    setHeight(idStorageBillRv.getMeasuredHeight());
                }
                return true;
            }
        });
    }

    public void setHeight(int height) {
        isMeasured = true;
        if (mAdapter == null && isAdded() && null != storageBillList) {
            mAdapter = new StorageBillAdapter(storageBillList, height, mActivity);
            idStorageBillRv.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void itemClickListener(int position, View view, StorageBillBean.StorageBillItemBean dataBean) {
        toast(position + dataBean.getUnitName());
        EventBus.getDefault().post(new StorageBillEvent(dataBean));
        popFragment();
    }
}
